package com.windoent.yugioh.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.StyleSpan;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.windoent.yugioh.R;
import com.windoent.yugioh.utils.BaseMethod;

/**
 * 作者：Android
 * 时间：2021/5/16 11:08
 * 功能：成功失败(1s消失)
 */
public class SucessFailAutoDismissDialog extends Dialog {

    private ImageView mImage;
    private TextView mTvContent;
    private TextView mTvWarning;

    private String messageStr;
    private String warningStr;
    private int resId;
    private boolean sucess = true;
    private int FLAG_DISMISS = 1;
    private boolean flag = true;
    private OnInterface listener;
    private String failReasonStr;

    public SucessFailAutoDismissDialog(Context context, OnInterface onInterface) {
        super(context, R.style.MoHUDialog);

        this.listener = onInterface;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.free_sucess_fail_auto_dismiss);
        //按空白处能取消动画
        setCanceledOnTouchOutside(true);
        Window window = getWindow();
        WindowManager.LayoutParams attributes = window.getAttributes();
        attributes.gravity = Gravity.CENTER;
        attributes.width = ViewGroup.LayoutParams.WRAP_CONTENT;
        attributes.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setGravity(Gravity.CENTER);
        window.setAttributes(attributes);

        //初始化界面控件
        initView();
        //初始化界面数据
        initData();
        //初始化界面控件的事件
        initEvent();

    }

    /**
     * 初始化界面的确定和取消监听器
     */
    private void initEvent() {
        //设置确定按钮被点击后，向外界提供监听

    }

    /**
     * 初始化界面控件的显示数据
     */
    private void initData() {
        if (BaseMethod.notNull(messageStr)) {
            mTvContent.setText(messageStr);
        }
        if (resId != 0) {
            mImage.setBackgroundResource(resId);
        }
        if (BaseMethod.notNull(warningStr) && BaseMethod.isNull(failReasonStr)) {
            SpannableString textSpanned2 = new SpannableString(warningStr);
            textSpanned2.setSpan(new StyleSpan(Typeface.BOLD),
                    0, warningStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTvWarning.setText(textSpanned2);
        } else if (BaseMethod.notNull(warningStr) && BaseMethod.notNull(failReasonStr)) {//富文本
            SpannableStringBuilder textSpannedBuilder2 = new SpannableStringBuilder();
            SpannableString textSpanned2 = new SpannableString(warningStr);
            textSpanned2.setSpan(new StyleSpan(Typeface.BOLD),
                    0, warningStr.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            mTvWarning.setText(textSpannedBuilder2.append(textSpanned2).append(failReasonStr));
            mTvWarning.setGravity(Gravity.LEFT);
        } else {
            mTvWarning.setText(failReasonStr);
            mTvWarning.setGravity(Gravity.CENTER);
        }

        mTvWarning.setVisibility(BaseMethod.notNull(warningStr) || BaseMethod.notNull(failReasonStr) ? View.VISIBLE : View.GONE);
        mTvContent.setTextColor(sucess ? Color.parseColor("#FF26B156") : Color.parseColor("#FFEC4E4E"));
    }

    /**
     * 初始化界面控件
     */
    private void initView() {
        mImage = (ImageView) findViewById(R.id.image);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mTvWarning = (TextView) findViewById(R.id.tv_warning);

    }

    public void setTvContent(boolean bSucess, int res, String contenstr, String warnStr, String failReason) {
        this.messageStr = contenstr;
        this.warningStr = warnStr;
        this.resId = res;
        this.sucess = bSucess;
        this.failReasonStr = failReason;
    }

    @Override
    public void show() {
        super.show();
        mThread.start();
    }

    private Thread mThread = new Thread() {
        @Override
        public void run() {
            super.run();
            while (flag) {
                try {
                    Thread.sleep(1000);
                    Message msg = mHandler.obtainMessage();
                    msg.what = FLAG_DISMISS;
                    mHandler.sendMessage(msg);
                    flag = false;
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == FLAG_DISMISS) {
                if (listener != null) {
                    listener.onDissmiss();
                }
                dismiss();
            }
        }

    };

    public interface OnInterface {
        void onDissmiss();
    }
}
